package cn.cvs.service.forumcomment;

import cn.cvs.dao.forumcomment.ForumCommentMapper;
import cn.cvs.dao.user.UserMapper;
import cn.cvs.pojo.Comment;
import cn.cvs.pojo.ForumComment;
import cn.cvs.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ForumCommentServiceImpl implements  ForumCommentService {
    @Autowired
    ForumCommentMapper forumCommentMapper;

    @Autowired
    private UserMapper userDao;


    @Override
    public List<ForumComment> getForumCommentById(Integer id) {
        List<ForumComment> topLevelComments = forumCommentMapper.getForumCommentByForumId(id);
        for (ForumComment forumComment : topLevelComments) {
            User user = userDao.selectById(forumComment.getUserId());
            forumComment.setUser(user);
            findForumComments(forumComment);
        }
        return topLevelComments;
    }

    @Override
    public int addForum(ForumComment forumComment) {
        return forumCommentMapper.addForum(forumComment);
    }

    private void findForumComments(ForumComment forumComment) {
        Integer commentId = forumComment.getId();
        List<ForumComment> replies = forumCommentMapper.getForumCommentsByParentId(commentId);
        for (ForumComment reply : replies) {
            User user = userDao.selectById(reply.getUserId());
            reply.setUser(user);
            forumComment.getForumCommentList().add(reply);
            findForumComments(reply);

        }
    }
}
